/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit.ieproject.presentation;

import java.awt.Color;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import uit.ieproject.business.RegexBLO;
import uit.ieproject.entities.Regex;
import uit.ieproject.utilites.StripedTableCellRenderer;
import uit.ieproject.utilites.TableUtil;

/**
 *
 * @author Maivia
 */
public class ListRegexFrame extends javax.swing.JFrame {

    private RegexBLO regexBLO = new RegexBLO();

    /**
     * Creates new form ListRegexFrame
     */
    public ListRegexFrame() {
        initComponents();
        this.setLocationRelativeTo(null);
        
        //Set icon image:
        ImageIcon icon = new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/Regex48.png"));
        this.setIconImage(icon.getImage());

        resetButtonStatus();
        try {
            //Show list regexs
            ArrayList<Regex> regexs = regexBLO.getAllRegexs();
            showRegex(tbListRegex, regexs);
            //Set font and Alignment text in jtable:
            TableUtil.formatTable(tbListRegex);
            StripedTableCellRenderer.installInTable(tbListRegex, Color.getHSBColor(1, 100, 217), null, null, null);
        } catch (SQLException ex) {
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbTypeSearchRegex = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        cbNameSearchRegex = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        cbStatusRegex = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btSearchRegex = new javax.swing.JButton();
        btViewAllRegex = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tbListRegex = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        btEditRegex = new javax.swing.JButton();
        btDeleteRegex = new javax.swing.JButton();
        btActiveRegex = new javax.swing.JButton();
        btDeactiveRegex = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Danh sách biểu thức chính quy");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        cbTypeSearchRegex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Câu hỏi", "Rút trích", "Tất cả" }));
        cbTypeSearchRegex.setSelectedIndex(2);

        jLabel8.setText("Loại:");

        cbNameSearchRegex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Năm sinh", "Tên thật", "Tên hiệu", "Quê quán", "Tên cha", "Tên mẹ", "Quá trình hoạt động", "Tất cả" }));
        cbNameSearchRegex.setSelectedIndex(7);

        jLabel9.setText("Tên:");

        cbStatusRegex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chưa kích hoạt", "Kích hoạt", "Tất cả" }));
        cbStatusRegex.setSelectedIndex(2);

        jLabel10.setText("Trạng thái:");

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, java.awt.Color.white, null));

        btSearchRegex.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/search.png"))); // NOI18N
        btSearchRegex.setText("Tìm kiếm");
        btSearchRegex.setMaximumSize(new java.awt.Dimension(112, 34));
        btSearchRegex.setMinimumSize(new java.awt.Dimension(112, 34));
        btSearchRegex.setPreferredSize(new java.awt.Dimension(112, 34));
        btSearchRegex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSearchRegexActionPerformed(evt);
            }
        });

        btViewAllRegex.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/all.png"))); // NOI18N
        btViewAllRegex.setText("Xem tất cả");
        btViewAllRegex.setPreferredSize(new java.awt.Dimension(124, 34));
        btViewAllRegex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btViewAllRegexActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btSearchRegex, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btViewAllRegex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSearchRegex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btViewAllRegex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbNameSearchRegex, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbStatusRegex, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbTypeSearchRegex, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(12, 12, 12)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(cbTypeSearchRegex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbNameSearchRegex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(cbStatusRegex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setText("Tra cứu biểu thức chính quy");

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        tbListRegex.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Tên", "Loại", "Biểu thức", "Trạng thái", "Mã"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Long.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbListRegex.setGridColor(new java.awt.Color(153, 204, 255));
        tbListRegex.setRowHeight(18);
        tbListRegex.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbListRegexMousePressed(evt);
            }
        });
        jScrollPane4.setViewportView(tbListRegex);
        tbListRegex.getColumnModel().getColumn(0).setPreferredWidth(25);
        tbListRegex.getColumnModel().getColumn(1).setPreferredWidth(100);
        tbListRegex.getColumnModel().getColumn(2).setPreferredWidth(50);
        tbListRegex.getColumnModel().getColumn(3).setPreferredWidth(400);
        tbListRegex.getColumnModel().getColumn(4).setPreferredWidth(85);
        tbListRegex.getColumnModel().getColumn(5).setMinWidth(0);
        tbListRegex.getColumnModel().getColumn(5).setPreferredWidth(0);
        tbListRegex.getColumnModel().getColumn(5).setMaxWidth(0);

        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        btEditRegex.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/edit (2).png"))); // NOI18N
        btEditRegex.setText("Thay đổi");
        btEditRegex.setEnabled(false);
        btEditRegex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditRegexActionPerformed(evt);
            }
        });

        btDeleteRegex.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/Remove24.png"))); // NOI18N
        btDeleteRegex.setText("Xóa");
        btDeleteRegex.setEnabled(false);
        btDeleteRegex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeleteRegexActionPerformed(evt);
            }
        });

        btActiveRegex.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/active24.png"))); // NOI18N
        btActiveRegex.setText("Kích hoạt");
        btActiveRegex.setEnabled(false);
        btActiveRegex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btActiveRegexActionPerformed(evt);
            }
        });

        btDeactiveRegex.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uit/ieproject/images/deactive24.png"))); // NOI18N
        btDeactiveRegex.setText("Tắt kích hoạt");
        btDeactiveRegex.setEnabled(false);
        btDeactiveRegex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeactiveRegexActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(btEditRegex, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btDeleteRegex, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btActiveRegex, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btDeactiveRegex, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel6Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btActiveRegex, btDeactiveRegex, btDeleteRegex, btEditRegex});

        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btDeleteRegex)
                    .addComponent(btEditRegex)
                    .addComponent(btActiveRegex, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btDeactiveRegex, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel2.setText("Danh sách biểu thức chính quy");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void resetButtonStatus() {
        btEditRegex.setEnabled(false);
        btDeleteRegex.setEnabled(false);
        btActiveRegex.setEnabled(false);
        btDeactiveRegex.setEnabled(false);
    }

    

    private int checkIsActive(int[] rows) {
        int result = 0;
        int numActive = 0;
        int numDeActive = 0;
        int length = rows.length;
        for (int i = 0; i < length; i++) {
            String status = tbListRegex.getValueAt(rows[i], 4).toString();
            if (status.contains("Chưa")) {
                numDeActive++;
            } else {
                numActive++;
            }
        }
        if (numActive != 0 && numDeActive != 0) {
            result = 2;
        } else if (numActive == 0) {
            result = 1;
        } else if (numDeActive == 0) {
            result = 0;
        }
        return result;
    }

    private Regex setRegexFromSelectedItem(int row, JTable table) {
        //Get information of clicked item:
        Long idSelected = Long.valueOf(table.getValueAt(row, 5).toString());
        String name = table.getValueAt(row, 1).toString();
        String type = table.getValueAt(row, 2).toString();
        String expression = table.getValueAt(row, 3).toString();
        String status = table.getValueAt(row, 4).toString();
        int isActive;
        if (status.contains("Chưa")) {
            isActive = 0;
        } else {
            isActive = 1;
        }
        Regex regexSelected = new Regex();
        regexSelected.setId(idSelected);
        regexSelected.setName(name);
        regexSelected.setType(type);
        regexSelected.setExpression(expression);
        regexSelected.setIsActive(isActive);
        return regexSelected;
    }

    private void showRegex(JTable table, ArrayList<Regex> regexs) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setNumRows(0);
        int numRegexs = regexs.size();
        if (numRegexs > 0) {
            for (int i = 0; i < numRegexs; i++) {
                Regex regex = regexs.get(i);
                String status;
                if (regex.getIsActive() == 0) {
                    status = "Chưa kích hoạt";
                } else {
                    status = "Đã kích hoạt";
                }
                model.addRow(new Object[]{i + 1, regex.getName(), regex.getType(), regex.getExpression(), status, regex.getId()});
            }
        }
    }

    private void btViewAllRegexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btViewAllRegexActionPerformed
        // TODO add your handling code here:
        cbNameSearchRegex.setSelectedIndex(7);
        cbStatusRegex.setSelectedIndex(2);
        cbTypeSearchRegex.setSelectedIndex(2);
        try {
            ArrayList<Regex> regexs = regexBLO.getAllRegexs();
            showRegex(tbListRegex, regexs);
        } catch (SQLException ex) {
        }finally{
            resetButtonStatus();
        }
    }//GEN-LAST:event_btViewAllRegexActionPerformed

    private void btSearchRegexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSearchRegexActionPerformed
        // TODO add your handling code here:
        try {
            DefaultTableModel model = (DefaultTableModel) this.tbListRegex.getModel();
            model.setNumRows(0);
            String name = cbNameSearchRegex.getSelectedItem().toString();
            String type = cbTypeSearchRegex.getSelectedItem().toString();
            int status = cbStatusRegex.getSelectedIndex();
            ArrayList<Regex> regexs = regexBLO.getRegex(name, type, status);
            if (regexs.size() > 0) {
                showRegex(tbListRegex, regexs);
            } else {
                JOptionPane.showMessageDialog(null, "Không tìm thấy");
            }
        } catch (SQLException ex) {
        }finally{
            resetButtonStatus();
        }
    }//GEN-LAST:event_btSearchRegexActionPerformed

    private void btDeleteRegexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeleteRegexActionPerformed
        // TODO add your handling code here:
        try {
            int dialogResult = JOptionPane.showConfirmDialog(null, "Bạn có chắc chắn muốn xóa không?", "Warning", JOptionPane.YES_NO_OPTION);
            if (dialogResult == JOptionPane.YES_OPTION) {
                long[] idsSelected = TableUtil.getIdsSelected(tbListRegex,5);
                regexBLO.deleteRegex(idsSelected);
                ArrayList<Regex> regexs = regexBLO.getAllRegexs();
                showRegex(tbListRegex, regexs);
            }
        } catch (SQLException ex) {
        } finally {
            resetButtonStatus();
        }
    }//GEN-LAST:event_btDeleteRegexActionPerformed

    private void btEditRegexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditRegexActionPerformed
        // TODO add your handling code here:
        //Edit mode:
        int row = tbListRegex.getSelectedRow();
        if (row < 0) {
            return;
        }
        //Get information of clicked item:
        Regex regexSelected = setRegexFromSelectedItem(row, tbListRegex);
        this.setVisible(false);
        MainFrame currentMainFrame = (MainFrame) JFrame.getFrames()[1];
        currentMainFrame.editModeWithRegex(regexSelected);
    }//GEN-LAST:event_btEditRegexActionPerformed

    private void btActiveRegexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btActiveRegexActionPerformed
        // TODO add your handling code here:
        try {
            long[] idsSelected = TableUtil.getIdsSelected(tbListRegex,5);
            regexBLO.activeRegexs(idsSelected, 1);
            ArrayList<Regex> regexs = regexBLO.getAllRegexs();
            showRegex(tbListRegex, regexs);
        } catch (SQLException ex) {
        } finally {
            resetButtonStatus();
        }
    }//GEN-LAST:event_btActiveRegexActionPerformed

    private void btDeactiveRegexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeactiveRegexActionPerformed
        // TODO add your handling code here:
        try {
            long[] idsSelected = TableUtil.getIdsSelected(tbListRegex,5);
            regexBLO.activeRegexs(idsSelected, 0);
            ArrayList<Regex> regexs = regexBLO.getAllRegexs();
            showRegex(tbListRegex, regexs);
        } catch (SQLException ex) {
        } finally {
            resetButtonStatus();
        }
    }//GEN-LAST:event_btDeactiveRegexActionPerformed

    private void tbListRegexMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbListRegexMousePressed
        JTable table = (JTable) evt.getSource();        
        if ((evt.getClickCount() == 1)) {
            int[] selectedRows = table.getSelectedRows();
            if (selectedRows.length == 1) {
                btEditRegex.setEnabled(true);
                btDeleteRegex.setEnabled(true);
                if (checkIsActive(selectedRows) == 1) {
                    btActiveRegex.setEnabled(true);
                    btDeactiveRegex.setEnabled(false);
                } else if (checkIsActive(selectedRows) == 0) {
                    btActiveRegex.setEnabled(false);
                    btDeactiveRegex.setEnabled(true);
                }
            } else {
                btEditRegex.setEnabled(false);
                if (checkIsActive(selectedRows) == 2) {
                    btActiveRegex.setEnabled(true);
                    btDeactiveRegex.setEnabled(true);
                } else if (checkIsActive(selectedRows) == 1) {
                    btActiveRegex.setEnabled(true);
                    btDeactiveRegex.setEnabled(false);
                } else {
                    btActiveRegex.setEnabled(false);
                    btDeactiveRegex.setEnabled(true);
                }
            }
        } else {    //Double click
            //Edit mode:
            int row = table.getSelectedRow();
            if (row < 0) {
                return;
            }
            //Get information of clicked item:
            Regex regexSelected = setRegexFromSelectedItem(row, table);

            this.setVisible(false);
            MainFrame currentMainFrame = (MainFrame) JFrame.getFrames()[1];
            currentMainFrame.editModeWithRegex(regexSelected);
        }
    }//GEN-LAST:event_tbListRegexMousePressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btActiveRegex;
    private javax.swing.JButton btDeactiveRegex;
    private javax.swing.JButton btDeleteRegex;
    private javax.swing.JButton btEditRegex;
    private javax.swing.JButton btSearchRegex;
    private javax.swing.JButton btViewAllRegex;
    private javax.swing.JComboBox cbNameSearchRegex;
    private javax.swing.JComboBox cbStatusRegex;
    private javax.swing.JComboBox cbTypeSearchRegex;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable tbListRegex;
    // End of variables declaration//GEN-END:variables
}
